<template>
    <!-- 添加或修改订单产品工序分配任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-tabs v-model="activeName" class="detail-tab" type="card">
                <el-tab-pane label="基本信息" name="first">
                    <el-row :gutter="24">
                      <el-col :span="12">
                          <el-form-item label="工人" prop="userId" style="margin-top: 30px;" required>
                            <el-select v-model="form.userId" placeholder="请选择工人" filterable @change="selectUser" clearable>
                              <el-option v-for="dict in dict.type.sys_user" ref="myselectUser"
                                         :key="dict.value" :label="dict.label" :value="dict.value"/>
                            </el-select>
                          </el-form-item>
                      </el-col>
                      <el-col :span="12" style="margin-top: 30px;">
                          <el-form-item label="分配数量" prop="count" required>
                            <el-input-number v-model="form.count" :max="counts" @change="feipeiCount" placeholder="请输入分配数量"/>
                          </el-form-item>
                        </el-col>
                    </el-row>
                  <el-row :gutter="24">
                        <!--<el-col :span="12">
                          <el-form-item label="实际完成时间" prop="doEndTime">
                                <el-date-picker clearable
                                                v-model="form.doEndTime"
                                                type="date"
                                                value-format="yyyy-MM-dd"
                                                placeholder="请选择实际完成时间">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>-->
                        <el-col :span="12" style="margin-top: 30px;">
                            <el-form-item label="计划完成时间" prop="planEndTime">
                                <el-date-picker clearable
                                                v-model="form.planEndTime"
                                                type="datetime"
                                                value-format="yyyy-MM-dd HH:mm:ss"
                                                placeholder="请选择计划完成时间">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12" style="margin-top: 30px;">
                            <el-form-item label="单价" prop="money">
                                <el-input v-model="form.money" placeholder="请输入单价" disabled/>
                            </el-form-item>
                        </el-col>
                  </el-row>
                  <el-row :gutter="24" style="margin-top: 30px;">
                        <el-col :span="12">
                            <el-form-item label="总价" prop="allMoney">
                                <el-input v-model="form.allMoney" placeholder="请输入总价" disabled/>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-tab-pane>
            </el-tabs>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import { addAction, getById, updateAction } from '@/api/manage';
    import moment from 'moment';

    export default {
        name: "AddUseFormList",
        dicts: ['sys_user'],
        data() {
            return {
                // 遮罩层
                loading: true,
                activeName: "first",
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                parentId: null,
                counts: null,
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    userId: [
                        { required: true, message: '请选择工人', trigger: 'change' }
                    ],
                    count: [
                        { required: true, message: '请输入分配数量', trigger: 'blur' },
                        { validator: (rule, value, callback) => {
                            if (value <= 0) {
                              callback(new Error('分配数量不能为0'));
                            } else {
                              callback();
                            }
                          }, trigger: 'blur' }
                    ]
                },
                url: {
                    getById:'/goodsSaledoGongxuUser/goodsSaledoGongxuUser/',
                    addUserTask:'/goodsSaledoGongxuUser/goodsSaledoGongxuUser/addUserTask',
                    edit:'/goodsSaledoGongxuUser/goodsSaledoGongxuUser/edit',
                },
            };
        },
        created() {

        },
        methods: {
            selectUser(value) {
              let that = this;
              that.$nextTick(() => {
                let arr = that.$refs.myselectUser;
                for (let i = 0; i < arr.length; i++) {
                  if (arr[i].currentValue == value) {
                    that.form.userName = arr[i].label;
                  }
                }
              })
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                  id: null,
                  xuhao: null,
                  code: null,
                  name: null,
                  delFlag: null,
                  createBy: null,
                  createTime: null,
                  updateBy: null,
                  updateTime: null,
                  remark: null,
                  shuoming: null,
                  status: "0",
                  projectId: null,
                  projectCode: null,
                  projectName: null,
                  saleId: null,
                  goodsId: null,
                  gongyiId: null,
                  gongxuId: null,
                  gongyiCode: null,
                  goodsCode: null,
                  goodsName: null,
                  goodsModel: null,
                  goodsBrand: null,
                  doStartTime: null,
                  doEndTime: null,
                  planStartTime: null,
                  planEndTime: null,
                  userId: null,
                  userName: null,
                  money: null,
                  count: null,
                  allMoney: null,
                  remainCount: null,
                  parentId: null
                };
                this.resetForm("form");
            },
            /** 新增按钮操作 */
            addUserShow(remainCount,parentId,money) {
                this.reset();
                this.form.parentId = parentId;
                this.form.count = '';
                this.form.money = money;
                this.form.allMoney = money*remainCount;
                this.counts = remainCount;
                this.open = true;
                this.title = "添加订单产品工序分配任务";
            },
            feipeiCount() {
              let that = this;
              if (that.form.money && that.form.count) {
                let totlePrice = this.functionNumberMul(that.form.count, that.form.money);
                that.$set(that.form, "allMoney", totlePrice);
              }
            },
            functionNumberMul(arg1, arg2) {
              var m = 0;
              var s1 = arg1.toString();
              var s2 = arg2.toString();
              try {
                m += s1.split(".")[1].length;
              } catch (e) {
              }
              try {
                m += s2.split(".")[1].length;
              } catch (e) {
              }
              return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids;
                getById(this.url.getById, id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改订单产品工序分配任务";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateAction(this.url.edit, this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        } else {
                            addAction(this.url.addUserTask, this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.$emit('ok');
                            });
                        }
                    }
                });
            },
        }
    };
</script>
